/*
 * Created By Visual Studio 2005
 * Create by: daqingshu
 * Create at: 2006-10-18 20:45:12{DATE}
 * Organization: BIT
 * 
 *
 */


using System;
using System.Collections.Generic;
using System.Text;
using Adge.IDAL;
using Adge.Models;
using NHibernate.Expression;

namespace Adge.BLL
{
    public class AdgeArBase<T> where T : class
    {
        private const string ERROR_PREFIX = "Adge.BLL.ExceptionReporter:";
        private log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static readonly IAdgeArBase<T> dal = Adge.DALFactory.DataAccess<T>.CreateEntity();

        public bool Create(T info)
        {
            try
            {
                return dal.Create(info);
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
                return false;
            }
        }

        public bool Update(T info)
        {
            try
            {
                return dal.Update(info);
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
                return false;
            }
        }
        
        public bool Save(T info)
        {
            try
            {
                return dal.Save(info);
            }
            catch(Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
                return false;
            }
        }

        public bool Delete(object objId)
        {
            try
            {
                return dal.Delete(objId);
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
                return false;
            }
        }

        public T GetEntityById(object objId)
        {
            T subject;
            try
            {
                subject = dal.GetEntityById(objId);
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
                return null;
            }
            return subject;
        }

        public T GetModel(object objId)
        {
            return GetEntityById(objId);
        }        

        public T[] FindAll()
        {
            try
            {
                return dal.FindAll();
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
                return new T[] { };
            }
        }

        public T[] SlicedFindAll(int firstResult, int maxResults, Order[] orders, ICriterion[] criterias)
        {
            try
            {
                return dal.SlicedFindAll(firstResult, maxResults, orders, criterias);
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
                return new T[] { };
            }
        }

        public bool DeleteAll()
        {
            try
            {
                return dal.DeleteAll();
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
                return false;
            }
        }
    }
}
